package com.qxf.util.db;

/**
 * <p>
 * Title: DbClass
 * </p>
 * <p>
 * Description: ������ݿ�l��
 * </p>
 * <p>
 * Copyright: Copyright (c) 2004
 * </p>
 * <p>
 * Company: company
 * </p>
 * 
 * @author Jeff Ding
 * @version 1.0
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException; 

public class DbClass {  

    private static DataSource ds = null; 

    private static String dbName = "db_adclickmgt";

    private Connection dbConn;
    
    private String url = "";

    public DbClass(String _dbName,boolean needds) {
    	if(needds)
    	{
	    	boolean newDS = false;
	        if (!dbName.equals(_dbName) || ds == null) {
	        	if(_dbName!=null&&!"".equals(_dbName)){
	        		dbName = _dbName;
	        		newDS = true;
	        	}
	        	if(ds == null||newDS){
		            try {
		                Context ctx = (Context) new InitialContext().lookup("java:comp/env");
		                ds = (DataSource) ctx.lookup("jdbc/"+dbName);
		            } catch (NamingException e) {
		                e.printStackTrace();
		            }
	        	}
	        }
    	}else
    	{
    		try{
    	    	Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance(); 
//    	    	this.url="jdbc:jtds:sqlserver://web202605.host22.chinajsp.net:1433/web202605;USER=web202605;PASSWORD=wzw81217";
    	    	this.url="jdbc:jtds:sqlserver://localhost:1433/db_adclickmgt;USER=sa;PASSWORD=";
    	    	} 
    	    	catch (Exception e) {
    	            System.err.println(e.getMessage());
    	    	}
    	}
       
    	
    	
    	
    }
    
    
    public DbClass(){
    	new DbClass(null,true);
    }

    /***************************************************************************
     * 
     * Method Name: getConnection <br>
     * Description: ִ��l����ݿ� <br>
     * 
     * @return: �õ���ݿ�l��DbConn <br>
     */
    //*****************************************************************************
    public Connection getConnection(boolean needds) {
        disconnect();
        try {
        	if(needds)
        	{
        		dbConn = ds.getConnection();
        	}else
        	{
        		
        		dbConn = DriverManager.getConnection(url);
        	}
            return dbConn;
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    /***************************************************************************
     * 
     * Method Name: disconnect <br>
     * Description: �ر���ݿ�l�� <br>
     *  
     */
    //*****************************************************************************
    public void disconnect() {
        if (dbConn != null) {
            try {
                dbConn.close();
            } catch (SQLException e) {
                System.err.println(e.getMessage());
            }
            dbConn = null;
        }
    }
    
    public static void main(String[] args){
    	
    }
    
}